package com.input;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;

public class FileInput {

	public static void println(String output) {
		System.out.println(output);
	}	
	
	//reading file before j2se 5.0
	public static void readFile_version1(String filename) {
		
		try {
			File file = new File(filename);
			FileReader reader = new FileReader(file);
			BufferedReader buffer = new BufferedReader(reader);
			String lines;
			while ((lines = buffer.readLine()) != null) {
				println(lines);
			}
			
		} catch (FileNotFoundException e) {
			
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
	
	
	//this version uses Scanner class for reading file 
	//In previous version we actually used the FileReader and BufferedReader
	public static void readFile_version2(String filename){
		try {
			File file = new File(filename);
			Scanner scan = new Scanner(file);
			while (scan.hasNext()) {
				println(scan.nextLine());
				
			}
			scan.close();
		} catch (IOException e) {
			// TODO: handle exception
		}
	}
	
	
/*
 * TextScanner creates a Scanner object from the File. 
 * The Scanner breaks the contents of the File into tokens 
 * using a delimiter pattern, By default the delimiter pattern is whitespace. 
 * TextScanner then calls the hasNext() method in Scanner. 
 * This method returns true if another token exists in the Scanner's input, 
 * which is the case until it reaches the end of the file. 
 * The next() method returns a String that represents the next token. 
 * So until it reaches the end of the file, TextScanner prints the String 
 * returned by next() on a separate line.

You can change the delimeter that is used to tokenize the input, 
through the useDelimiter method of Scanner. You can pass in a String or 
a java.util.regex.Pattern to the method. See the JavaDocs page for Pattern 
for information on what patterns are appropriate. For example, you can read
 the input one line at a time by using the newline character (\n) as a delimiter. 
 Here is the revised readFile() method for TextScanner that uses a newline character 
 as the delimiter:
 */
	
	public static void scanner_withDelimiter() {
		
	}
	
	
	public static void main(String[] args) {
		String filename = "input.in";
		//remember to keep this file outside the source folder or any folder other than source folder.
		
		//readFile_version1(filename);
		readFile_version2(filename);
	}
}
